package com.wu.controller;

import com.wu.mapper.MenuMapper;
import com.wu.mapper.TypeMapper;
import com.wu.pojo.Menu;
import com.wu.pojo.Type;
import com.wu.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class menuController {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index(){
        return this.port;
    }

    @GetMapping("/findall/{index}/{limit}")
    public MenuVo findAll(@PathVariable("index") int index,@PathVariable("limit") int limit){
        //1.由于Layui框架的数据格式，所以我们要对这个进行一个封装
        MenuVo menuVo = new MenuVo();
        menuVo.setCode(0).setMsg("数据格式有误").setCount(menuMapper.count()).setData(menuMapper.findAll(index, limit));
        return menuVo;

    }

    //2.删除操作
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        menuMapper.deleteById(id);
    }

    //3.查询菜品所属系列操作
    @GetMapping("/findTypes")
    public List<Type> findTypes(){
        return typeMapper.findTypes();
    }

    //4.保存方法
    @PostMapping("/save")
    public void save(@RequestBody Menu menu){
        menuMapper.save(menu);
    }

    //5.查询一个
    @GetMapping("/findById/{id}")
    public Menu findById(@PathVariable("id")long id){
        return menuMapper.findById(id);
    }

    //6.更新方法
    @PutMapping("/update")
    public void update(@RequestBody Menu menu){
        menuMapper.update(menu);
    }
}
